<?php if (null !== $block->getLicenseId()): ?>
<script src = "<?php echo $block->getUrl('livechat/getvisitor'); ?>"></script>
<script>
  window.__lc = window.__lc || {};
  window.__lc.license = <?php echo $block->getLicenseId() ?>;

  if(livechat_visitor_data) {
    window.__lc.visitor = livechat_visitor_data;
  }

  (function() {
    var lc = document.createElement('script'); lc.type = 'text/javascript'; lc.async = true;
    lc.src = ('https:' == document.location.protocol ? 'https://' : 'http://') + 'cdn.livechatinc.com/tracking.js';
    var s = document.getElementsByTagName('script')[0]; s.parentNode.insertBefore(lc, s);
  })(); 

  <?php if ($block->isCartSet()): ?>
    var LC_API = LC_API || {};
    LC_API.on_after_load = function() {
      getCart();
      setInterval(function(){
        getCart();
      }, 30000);
    }

    function getCart() {
      var xhttp = new XMLHttpRequest();
      xhttp.onreadystatechange = function() {
        if (this.readyState == 4 && this.status == 200) {
          LC_API.set_custom_variables(JSON.parse(this.responseText));
        }
      };
      xhttp.open("GET", "<?php echo $block->getUrl('livechat/getcart'); ?>"+"?success=<?php echo $block->isOrderPlacedPage() ?>", true);
      xhttp.send();
    }

  <?php endif; ?>
</script>
<?php else: ?>
<script>
  console.log('Please go to "Stores => Settings => Configuration" and configure your LiveChat account.');
</script>
<?php endif; ?>
